
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace Tanks
{
    public class Wall
    {
        Rectangle rect;
        public Rectangle Rect
        {
            get { return rect; }
        }

        Color Team;

        public bool is_base { get; set; }

        public string background { get; set; }

        Point left_up;//позиция верхнего левого угла
        public Point Left_up
        {
            get { return left_up; }
            set 
            { 
                left_up = value;
                refresh();
            }
        }

        Point right_down;//позиция нижнего правого угла
        public Point Right_down
        {
            get { return right_down; }
            set 
            {
                right_down = value;
                scale();
            }
        }

       


        public Wall(Point p1, Point p2)
        {
            left_up = p1;
            right_down = p2;
            is_base = false;
            background = "bricks_small.jpg";
            draw();            
        }

        public Wall(Point p1, string s)
        {
            left_up = p1;
            right_down.X = p1.X + 50;
            right_down.Y = p1.Y + 60;
            string st = "/Tanks;component/Images/";            
            st += s;
            background = s;
            is_base = true;
            if (s == "red_flag.png")
                Team = Colors.Red;
            else
                Team = Colors.Blue;
            draw_base(st);
        }

        public Double Height
        {
            get { return right_down.Y - left_up.Y; }
        }

        public Double Width
        {
            get { return right_down.X - left_up.X; }
        }

        void draw()//прорисовка
        {
            rect = new Rectangle();
            rect.Height = Math.Abs( right_down.Y - left_up.Y);
            rect.Width = Math.Abs ( right_down.X - left_up.X);
            ((App)Application.Current).Main_canvas.Children.Add(rect);
            Canvas.SetTop(rect,left_up.Y);
            Canvas.SetLeft(rect,left_up.X);
            ImageBrush im_br = new ImageBrush();
            if(rect.Width > rect.Height)
                im_br.ImageSource = new BitmapImage(new Uri("/Tanks;component/Images/bricks_small.jpg", UriKind.Relative));
            else
                im_br.ImageSource = new BitmapImage(new Uri("/Tanks;component/Images/bricks_small_rotate.jpg", UriKind.Relative));
            im_br.Stretch = Stretch.None;
            rect.Fill = im_br;
        }

        void draw_base(string s)//прорисовка базы
        {
            rect = new Rectangle();
            rect.Height = 60;
            rect.Width = 50;
            ((App)Application.Current).Main_canvas.Children.Add(rect);
            Canvas.SetTop(rect, left_up.Y);
            Canvas.SetLeft(rect, left_up.X);
            ImageBrush im_br = new ImageBrush();
            im_br.ImageSource = new BitmapImage(new Uri(s, UriKind.Relative));
            rect.Fill = im_br;
        }

        void scale()//увеличение (при прокрутке колесиком мыши)
        {
            rect.Width = right_down.X - left_up.X;
            rect.Height = right_down.Y - left_up.Y;
        }

        void refresh()//обновляет внешний вид стены на экране
        {
            Canvas.SetTop(rect, left_up.Y);
            Canvas.SetLeft(rect, left_up.X);
            right_down.X = left_up.X + rect.Width;
            right_down.Y = left_up.Y + rect.Height;
            
        }

        public void increase_damage(Direction d)//API для увеличения повреждения
        {
            if (d == Direction.Up)
            {
                if (rect.Height < 12)
                {
                    delete_from_canvas();
                    return;
                }
                right_down.Y -= 5;
                rect.Height -= 5;
            }
            if (d == Direction.Down)
            {
                if (rect.Height < 12)
                {
                    delete_from_canvas();
                    return;
                }
                left_up.Y += 5;
                rect.Height -= 5;
            }
            if (d == Direction.Right)
            {
                if (rect.Width < 12)
                {
                    delete_from_canvas();
                    return;
                }
                left_up.X += 5;
                rect.Width -= 5;
            }
            if (d == Direction.Left)
            {
                if (rect.Width < 12)
                {
                    delete_from_canvas();
                    return;
                }
                right_down.X -= 5;
                rect.Width -= 5;
            }
            refresh();
        }

        void delete_from_canvas()//удаляет из контейнера и карты
        {
            ((App)Application.Current).Main_canvas.Children.Remove(rect);
            ((App)Application.Current).map.List_of_walls.Remove(this);
            check_GameOver();
        }

        void check_GameOver()//проверяет, не уничтожена ли одна из  баз
        {
            if (is_base)
            {
                if (Team == Colors.Red)
                    ((App)Application.Current).MP.begin_new_game(Colors.Blue);
                else
                    ((App)Application.Current).MP.begin_new_game(Colors.Red);
            }
        }

    }
}


